//
//  BigInt.cpp
//  BigNum
//
//  Created by Sam Jaffe on 9/18/12.
//  Copyright (c) 2012 Sam Jaffe. All rights reserved.
//

#include "BigInt.h"

BigInt::operator std::string() const {
    return is_negative ? "-" : "" + std::string(*((BigNumber*)this));
}

void BigInt::sum(const BigInt& a, const BigInt& b) {
    if (a.is_negative and not b.is_negative)
        BigNumber::diff(b, a);
    else if (b.is_negative)
        BigNumber::diff(a, b);
    else {
        this->is_negative = a.is_negative;
        BigNumber::sum(a, b);
    }
}

void BigInt::diff(const BigInt& a, const BigInt& b) {
    bool g = (BigUInt) a > (BigUInt) b;
    if (a.is_negative and not b.is_negative) {
        this->is_negative = 1;
        BigNumber::sum(a, b);
    }
    else if (b.is_negative and not a.is_negative) {
        this->is_negative = 0;
        BigNumber::sum(a, b);
    }
    else {
        this->is_negative = g;
        if (g)
            BigNumber::diff(b, a);
        else
            BigNumber::diff(a, b);
    }
}

void BigInt::modulo(const BigInt& a, const BigInt& b) {
    if (a.is_negative or b.is_negative)
        throw "Modulus on Negative Integers is not Supported";
    BigNumber::modulo(a, b);
}

BigNumber BigInt::operator- () const {
    return BigInt(*this, !this->is_negative);
}
